﻿using Api.Core.Common.Extension;
using Api.Core.IRepository.IUnitOfWork;
using Api.Core.IRepository.Role;
using Api.Core.Model.Entity;
using Api.Core.Model.Table;
using Api.Core.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Api.Core.Repository.User
{
    public class GroupRoleRespository : BaseRepository<GroupRoleTable>, IGroupRoleRespository
    {
        protected readonly ISugarUnitOfWork _suger;
        public GroupRoleRespository(ISugarUnitOfWork sugarUnitOfWork) : base(sugarUnitOfWork)
        {
            _suger = sugarUnitOfWork;
        }

        /// <summary>
        /// 更新组权限
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> AddGroupRoles(GroupViewModel entity)
        {
            using (var client = _suger.GetDbClient())
            {
                try
                {
                    var oldGroupRoles = await client.Queryable<GroupRoleTable>().Where(x => x.GroupId == entity.GroupId).ToListAsync();
                    var newRoles = await client.Queryable<RoleTable>().Where(x => entity.RoleList.Select(x => x.RoleId).Contains(x.RoleId)).ToListAsync();

                    client.BeginTran();
                    var delResult = await client.Deleteable<GroupRoleEntity>(oldGroupRoles).ExecuteCommandAsync();
                    var mapGroupRoleList = newRoles.Select(x => new GroupRoleTable()
                    {
                        GroupId = entity.GroupId,
                        RoleId = x.RoleId
                    });

                    if (mapGroupRoleList != null && mapGroupRoleList.Any())
                    {
                        await client.Insertable(mapGroupRoleList.ToList()).ExecuteCommandAsync();
                    }
                    client.CommitTran();
                }
                catch (Exception e)
                {
                    client.RollbackTran();
                    return false;
                    throw e;
                }
            }
            return true;
        }

        /// <summary>
        /// 删除组权限
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> DelGroupRole(GroupViewModel entity)
        {
            var result = false;
            using (var client = _suger.GetDbClient())
            {
                var oldRoles = await client.Queryable<GroupRoleTable>().Where(x => x.GroupId == entity.GroupId && entity.RoleList.Select(x => x.RoleId).Contains(x.RoleId)).ToListAsync();
                if(oldRoles!=null && oldRoles.Any())
                {
                    result =  await client.Deleteable(oldRoles).ExecuteCommandHasChangeAsync();
                }
            }
            return result;
        }

        /// <summary>
        /// 按条件获取组
        /// </summary>
        /// <returns></returns>
        public async Task<List<GroupTable>> GetAllGroupsByCondition(GroupTable model)
        {
            using (var client = _suger.GetDbClient())
            {
                var result = await client.Queryable<GroupTable>()
                    .WhereIF(!model.GroupId.IsNullOrWhiteSpace(), x => x.GroupId == model.GroupId)
                    .ToListAsync();

                return result;
            }
        }

         
    }
}
